<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class smscode_model extends MY_Model {
	protected $table = 'smscode';
	public function sendCode($phone,$type=0,$ctype)
	{
		$now = time();
		$tmp = parent::get_one(array('phone'=>$phone,'type'=>$type, 'reload_time >'=>$now));

		if ($tmp) {
			$send_code_return['code'] = 421;
			$send_code_return['msg'] = '请再过'.( 60 -($tmp['reload_time']-$now) ).'秒请求短信';
			$send_code_return['phone'] = $phone;
			$send_code_return['auth_code'] = 0;
			return $send_code_return;
		}

		//echo ry_sendCode($phone,1);die();
		$send_info = send_authcode($phone,'',$ctype);
		if (!empty($send_info) && ($send_info['code'] == 0)) {

			$data = array('phone'=>$send_info['phone'],'type'=>$type, 'code'=>$send_info['auth_code'],'ip'=>get_ip(),'timeline'=>time() + SMSCODE_EXPIRES_TIME,'reload_time'=>time() + SMSCODE_RELOAD_TIME);
			if ($this->create($data)) {

				return $send_info;
			} else {
				return false;
			}
		} else {

			return false;
		}
	}

	public function verifySmsCode($phone, $code, $type)
	{
		$now = time();
		$expired = SMSCODE_EXPIRES_TIME;
		if (parent::get_one(array('phone'=>$phone, 'code'=>$code, 'timeline >'=>$now - $expired))) {
			// 验证后删除短信验证码
			$this->_delVerifySmsCode($phone);
			return true;
		} else {
			//if ((ENVIRONMENT == 'development') && ($code == '111111')) {
				//return true;
			//} else {
				return false;
			//}
		}
	}

	public function _delVerifySmsCode($phone)
	{
			$where_del = array('phone'=>$phone);
			parent::del('*', $where_del);
	}
}
